
<template>
  <el-form ref="form" :model="data" :rules="rules" label-width="100px">
          <el-form-item label="点位名称" prop="title">
              <el-input v-model.trim="data.title" placeholder="请填写点位名称" />
          </el-form-item>
        <el-form-item label="所属项目" prop="project_id">
          <el-select v-model="data.project_id" placeholder="请选择所属项目" clearable @change="select_project" class="filter-item">
            <el-option v-for="(project,index) in project_list" :key="index" :label="project.title" :value="project.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属建筑" prop="building_id">
          <el-select v-model="data.building_id" placeholder="请选择所属建筑"  @change="select_building"  clearable class="filter-item">
            <el-option v-for="(building,index) in building_list" :key="index" :label="building.title" :value="building.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属楼层" prop="floor_id">
          <el-select v-model.trim="data.floor_id" placeholder="请选择所属楼层" clearable  class="filter-item" >
            <el-option v-for="(floor,index) in floor_list" :key="index" :label="floor.title" :value="floor.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="排序" prop="sort">
            <el-input-number v-model="data.sort" :min="1" :max="10" label="排序"></el-input-number>
        </el-form-item>

    <el-dialog :visible.sync="dialogVisible" width="30%" append-to-body>
          <img width="100%" :src="previewUrl" alt="">
    </el-dialog>  
    <el-form-item>
      <el-button type="primary" :disabled="$store.state.settings.ajaxButton" @click="submit">提交</el-button>
    </el-form-item>
  </el-form>
</template>
<script>
import { create } from '@/api/inspectionpoints'
import { getToken } from '@/utils/auth'
import {commonApi} from '@/api/common'

export default {
  name: 'inspectionpointsCreate',
  components: { },
  props: {
    item: {
      type: Object,
      default: () => {
        return {}
      }
    },
    project_list: {
      type: Array,
      default: () => {
        return []
      }
    }

  },
  data() {
    return {
      loading:false,
      dialogVisible:false,
      previewUrl:'',
      headerObj: {
            'Authorization': 'Bearer ' + getToken()
        },
      rules: {
                    title: [ 
                { required: true, message: "请输入点位名称", trigger: "blur" }
            ], 
            project_id: [ 
                { required: true, message: "请输入所属项目", trigger: "blur" }
            ], 

      },
      building_list:[],
      floor_list:[],
      data: {
           title:'',
            project_id:'',
            building_id:'',
            floor_id:'',
            sort:'',

      }
    }
  },
  created() {
  },
  methods: {
    select_building(value){
      console.log(value)
      if (!value) {
        this.data.floor_id = '' // 清空楼层选择
        return
      }
      
      commonApi.get_floor({ building_id: value }).then(response => {
        this.floor_list = response.data
      }).catch(error => {
        console.error('获取楼层列表失败:', error)
        this.floor_list = []
      })
    },
    select_project(value) {
      console.log(value)
      if (!value) {
        this.building_list = []
        this.data.building_id = '' // 清空建筑选择
        return
      }
      
      commonApi.get_building({ project_id: value }).then(response => {
        this.building_list = response.data
      }).catch(error => {
        console.error('获取建筑列表失败:', error)
        this.building_list = []
      })
    },
    
    submit() {
 
      const thiz = this

      this.$refs.form.validate(valid => {
        if (!valid) {
          return false
        }
        create({
                       title:this.data.title,
            project_id:this.data.project_id,
            building_id:this.data.building_id,
            floor_id:this.data.floor_id,
            sort:this.data.sort,

        }).then(response => {
          this.$store.commit('settings/SET_AJAX_BUTTON', true);
          this.$message({
            message: '添加成功',
            type: 'success',
            duration: 1500,
            onClose() {
              if (thiz.$refs.form !== undefined) {
                thiz.$refs.form.resetFields()
              }
              thiz.item.dialogVisible = false
            }
          })
        })
      })
    }
  }
}
</script>

